import { menuRouter } from '@/router'
import { hasPermission, isPlatform } from '@/assets/js/js.js'
import { getAgentType } from './index.js'
let menus = []

export function clearMenus() {
	menus.splice(0)
}

export default function menu() {
	if(menus.length > 0) {
		return menus
	}
	menuRouter.forEach(router => {
					
		if(router.hidden) return
		
		let menu = {
			path : router.path,
			name : router.name,
			title : router.meta.title,
			icon : router.icon
		}
		
		if(!('children' in router)){ //没有子菜单的
			if(hasPermission(router.name)) {
				menus.push(menu)
			}
			
			return
		}
		
		menu.children = []
		
		if(router.children && router.children.length > 0){
			router.children.forEach((v, i) => {
				if(v.meta.ignore) {
					if(v.meta.ignore.includes('platform')) { //平台忽略
						if(isPlatform()) {
							return
						}
					}
				}
				
				if(v.hidden !== true){
					
					if(!hasPermission(v.name)) {
						return
					}
					
					menu.children.push({
						path : v.path,
						name : v.name,
						title : v.meta.title
					})
				}
			})
		}
		
		if(menu.children.length > 0) {
			if(menu.children.length == 1) {
				let _menu = menu.children[0]
				_menu.path = menu.path + '/' + _menu.path
				_menu.icon = menu.icon
				_menu.title = menu.title
				menus.push(_menu)
			}else{
				menus.push(menu)
			}
		}
		
	})
	return menus
}